package cn.wolfcode.p2p.base.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.wolfcode.p2p.base.domain.SystemDictionary;
import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.p2p.base.mapper.SystemDictionaryMapper;
import cn.wolfcode.p2p.base.query.SystemDictionaryQueryObject;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;

@Service
@Transactional
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
	@Autowired
	private SystemDictionaryMapper dicMapper;

	@Autowired
	private SystemDictionaryItemMapper itemMapper;

	@Override
	public PageInfo<SystemDictionary> queryDics(SystemDictionaryQueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<SystemDictionary> list = this.dicMapper.queryData(qo);
		return new PageInfo<>(list);
	}

	@Override
	public List<SystemDictionary> listAllDics() {
		return this.dicMapper.selectAll();
	}

	@Override
	public PageInfo<SystemDictionaryItem> queryItems(SystemDictionaryQueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<SystemDictionaryItem> list = this.itemMapper.queryData(qo);
		return new PageInfo<>(list);
	}

	@Override
	public void saveOrUpdate(SystemDictionary dic) {
		if (dic.getId() != null) {
			this.dicMapper.updateByPrimaryKey(dic);
		} else {
			this.dicMapper.insert(dic);
		}
	}

	@Override
	public void saveOrUpdateItem(SystemDictionaryItem item) {
		if (item.getId() != null) {
			this.itemMapper.updateByPrimaryKey(item);
		} else {
			this.itemMapper.insert(item);
		}
	}

	@Override
	public List<SystemDictionaryItem> listBySn(String sn) {
		return this.itemMapper.listBySn(sn);
	}

}
